//
//  QKVisitorView.m
//  QKWeibo-OC
//
//  Created by iMac_Kurt on 16/8/11.
//  Copyright © 2016年 Kurt_Qi. All rights reserved.
//

#import "QKVisitorView.h"
#import "UIButton+QKButton.h"
#import "UILabel+QKLabel.h"

@interface QKVisitorView ()
@property (nonatomic, strong) UIImageView *backIcon;
@property (nonatomic, strong) UIImageView *homeIcon;
@property (nonatomic, strong) UIImageView *maskIconView;
@property (nonatomic, strong) UILabel *messageLabel;

@end

@implementation QKVisitorView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 设置界面
        [self setupUI];
    }
    return self;
}

/// 设置访客视图信息
///
/// @param imageName 背景图片名称
/// @param text      消息文字内容
- (void)setupInfoWithImageName:(NSString *)imageName messageText:(NSString *)text {
    // 如果imageName为空字符串或为空对象时，表示为首页，则什么都不需要做
    if (imageName.length == 0 | imageName == nil) {
        [self startAnimationRotation];
    }else { // 非首页做下方设置
        self.backIcon.image = [UIImage imageNamed:imageName];
        self.messageLabel.text = text;
        self.homeIcon.hidden = YES;
        [self sendSubviewToBack:self.maskIconView];
    }
}

/// 开始旋转动画
- (void)startAnimationRotation {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI, 0, 0, 1)];
    animation.repeatCount = MAXFLOAT;
    animation.duration = 6.0;
    // 使动画连续不间断不跳帧循环
    animation.cumulative = YES;
    animation.removedOnCompletion = NO;
    
    [self.backIcon.layer addAnimation:animation forKey:nil];
}

/// 添加子控件
- (void)setupUI {
    [self addSubview:self.backIcon];
    
    // 渐变遮罩
    [self addSubview:self.maskIconView];
    
    [self addSubview:self.homeIcon];
    [self addSubview:self.messageLabel];
    [self addSubview:self.registButton];
    [self addSubview:self.loginButton];
    // 遍历设置子控件的属性
    for (UIView *v in self.subviews) {
        /*  translatesAutoresizingMaskIntoConstraints
         默认为YES，表示通过setframe方式来控制控件的位置
         如果需要通过添加自动布局来设置控件位置的时候，此属性需要设置为NO
         */
        v.translatesAutoresizingMaskIntoConstraints = NO;
    }
    
    // 设置背景颜色
    self.backgroundColor = [UIColor colorWithWhite:237.0 / 255.0 alpha:1.0];
    
}

/// 布局子控件
-(void)layoutSubviews {
    // backIcon
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.backIcon attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.backIcon attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:-40]];
    // homeIcon
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeIcon attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.backIcon attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.homeIcon attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.backIcon attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]];
    // messageLabel
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.backIcon attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.backIcon attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:224.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.messageLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:36.0]];
    // registButton
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:100.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.registButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:36.0]];
    // loginButton
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.messageLabel attribute:NSLayoutAttributeBottom multiplier:1.0 constant:10]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:100.0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.loginButton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:0 multiplier:1.0 constant:36.0]];
    // maskIconView
    // VFL：可视化标记语言
    // 注意：这里添加的是复数个约束，constraintsWithVisualFormat返回一个包含若干个NSLayoutConstraint对象的数组，它会自动把VFL字符串转换成若干个约束放到这个数组中返回
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_maskIconView]-0-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_maskIconView)]];
    [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_maskIconView]-buttonHeight-[_registButton]" options:0 metrics:@{@"buttonHeight": @-36.0} views:NSDictionaryOfVariableBindings(_maskIconView, _registButton)]];
}

#pragma mark - 懒加载控件
// 背景图
- (UIImageView *)backIcon {
    if (_backIcon == nil) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];

        _backIcon = imageView;
    }
    return _backIcon;
}
// 房子
- (UIImageView *)homeIcon {
    if (_homeIcon == nil) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
        
        _homeIcon = imageView;
    }
    return _homeIcon;
}
// 消息文字
- (UILabel *)messageLabel {
    if (_messageLabel == nil) {
        UILabel *label = [UILabel labelWithText:@"关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜" color:[UIColor darkGrayColor] fontSize:14];
        
        _messageLabel = label;
    }
    return _messageLabel;
}
// 注册按钮
- (UIButton *)registButton {
    if (_registButton == nil) {
        UIButton *button = [UIButton buttonWithTitle:@"注册" color:[UIColor orangeColor] backgroundImageName:@"common_button_white_disable"];
        _registButton = button;
    }
    return _registButton;
}
// 登录按钮
- (UIButton *)loginButton {
    if (_loginButton == nil) {
        UIButton *button = [UIButton buttonWithTitle:@"登录" color:[UIColor lightGrayColor] backgroundImageName:@"common_button_white_disable"];
        _loginButton = button;
    }
    return _loginButton;
}
// 渐变遮罩
- (UIImageView *)maskIconView {
    if (_maskIconView == nil) {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
        _maskIconView = imageView;
    }
    return _maskIconView;
}
@end
